package com.certificationManager.server.domain.dao.jdo;

import java.util.ArrayList;
import java.util.List;

import javax.jdo.PersistenceManager;

import org.apache.log4j.Logger;

import com.certificationManager.server.domain.dao.LanguageDAO;
import com.certificationManager.server.domain.manager.jdo.PMF;
import com.certificationManager.server.domain.model.Language;

/**
 * @author Benjamin Barbier
 *
 */
public class LanguageDAOJdo implements LanguageDAO {

	private static final LanguageDAOJdo singleton = new LanguageDAOJdo();
	
	public static LanguageDAOJdo getInstance() {return singleton;}
	
	/** A Bo should never be instantiated */
	private LanguageDAOJdo() {}
	
	/** Logger */
	private static final Logger logger = Logger.getLogger(LanguageDAOJdo.class); 
	
	
	/**
	 *  This Method Init all languages in database.
	 */
	public void initLanguages() {
		
		// Entity Manager Factory
		final PersistenceManager pm = PMF.get().getPersistenceManager();
		
		try {
			
			List<String[]> languages = new ArrayList<String[]>();
			languages.add(new String[]{"FR","Français"});
			languages.add(new String[]{"EN","English"});
			languages.add(new String[]{"DE","Deutsch"});
			languages.add(new String[]{"IT","Italiano"});
			languages.add(new String[]{"ES","Español"});
			
			for (String[] l:languages) {
				Language language = new Language(l[0], l[1]);
				pm.makePersistent(language);
			}
			
		} catch(Throwable t) {
			logger.debug("Unable to init Languages", t);			
		} finally {
			pm.close();	
		}
	
	}
	
}
